home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / dstring.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  7.3 KB  |  232 lines

  1. // DSTRING.H - Support for delphi strings in C++
  2. //            (AnsiString and template<sz> SmallString)
  3. // $Revision:   1.10.1.1  $
  4. // Copyright (c) 1997, 1998 Borland International
  5.  
  6. #ifndef DSTRING_H
  7. #define DSTRING_H
  8.  
  9. #if !defined(SYSDEFS_H)
  10. #include <sysdefs.h>
  11. #endif
  12.  
  13. namespace System
  14. {
  15.  
  16.   class PACKAGE TVarRec;
  17.   class DELPHIRETURN Currency;
  18.   class DELPHIRETURN WideString;
  19.  
  20.   class DELPHIRETURN AnsiString
  21.   {
  22.     friend PACKAGE AnsiString __fastcall operator +(const char*, const AnsiString& rhs);
  23.   public:
  24.     // the TStringFloatFormat enum is used by FloatToStrF
  25.     enum TStringFloatFormat
  26.     {sffGeneral, sffExponent, sffFixed, sffNumber, sffCurrency};
  27.     static AnsiString __fastcall StringOfChar(char ch, int count);
  28.     static AnsiString __fastcall LoadStr(int ident);
  29.     static AnsiString __fastcall FmtLoadStr(int ident, const TVarRec *args,
  30.                         int size);
  31.     static AnsiString __fastcall Format(const AnsiString& format,
  32.                       const TVarRec *args, int size);
  33.     static AnsiString __fastcall FormatFloat(const AnsiString& format,
  34.                          const long double& value);
  35.     static AnsiString __fastcall FloatToStrF(long double value,
  36.                          TStringFloatFormat format, int precision, int digits);
  37.     static AnsiString __fastcall IntToHex(int value, int digits);
  38.     static AnsiString __fastcall CurrToStr(Currency value);
  39.     static AnsiString __fastcall CurrToStrF(Currency value,
  40.                         TStringFloatFormat format, int digits);
  41.  
  42.     // Constructors
  43.     __fastcall AnsiString(): Data(0) {}
  44.     __fastcall AnsiString(const char* src);
  45.     __fastcall AnsiString(const AnsiString& src);
  46.     __fastcall AnsiString(const char* src, unsigned char len);
  47.     __fastcall AnsiString(const wchar_t* src);
  48.     __fastcall AnsiString(char src);
  49.     __fastcall AnsiString(short);
  50.     __fastcall AnsiString(unsigned short);
  51.     __fastcall AnsiString(int src);
  52.     __fastcall AnsiString(unsigned int);
  53.     __fastcall AnsiString(long);
  54.     __fastcall AnsiString(unsigned long);
  55.     __fastcall AnsiString(double src);
  56.     __fastcall AnsiString(const WideString &src);
  57.  
  58.     // Destructor
  59.     __fastcall ~AnsiString();
  60.  
  61.     // Assignments
  62.     AnsiString& __fastcall operator =(const AnsiString& rhs);
  63.     AnsiString& __fastcall operator +=(const AnsiString& rhs);
  64.  
  65.     // Comparisons
  66.     bool __fastcall operator ==(const AnsiString& rhs) const;
  67.     bool __fastcall operator !=(const AnsiString& rhs) const;
  68.     bool __fastcall operator <(const AnsiString& rhs) const;
  69.     bool __fastcall operator >(const AnsiString& rhs) const;
  70.     bool __fastcall operator <=(const AnsiString& rhs) const;
  71.     bool __fastcall operator >=(const AnsiString& rhs) const;
  72.     int __fastcall AnsiCompare(const AnsiString& rhs) const;
  73.     int __fastcall AnsiCompareIC(const AnsiString& rhs) const; //ignorecase
  74.  
  75.     // Index
  76.     char& __fastcall operator [](const int idx)
  77.     {
  78.       ThrowIfOutOfRange(idx);   // Should Range-checking be optional to avoid overhead ??
  79.       return Data[idx-1];
  80.     }
  81.  
  82.     // Concatenation
  83.     AnsiString __fastcall operator +(const AnsiString& rhs) const;
  84.  
  85.     // C string operator
  86.     char* __fastcall c_str() const
  87.     {return (Data)? Data: "";}
  88.  
  89.     // Query attributes of string
  90.     int __fastcall Length() const;
  91.     bool __fastcall IsEmpty() const;
  92.  
  93.     // Make string unique (refcnt == 1)
  94.     AnsiString&  __fastcall Unique();
  95.  
  96.     // Modify string
  97.     AnsiString&  __fastcall Insert(const AnsiString& str, int index);
  98.     AnsiString&  __fastcall Delete(int index, int count);
  99.     AnsiString&  __fastcall SetLength(int newLength);
  100.  
  101.     int __fastcall Pos(const AnsiString& subStr) const;
  102.     AnsiString __fastcall LowerCase() const;
  103.     AnsiString __fastcall UpperCase() const;
  104.     AnsiString __fastcall Trim() const;
  105.     AnsiString __fastcall TrimLeft() const;
  106.     AnsiString __fastcall TrimRight() const;
  107.     AnsiString __fastcall SubString(int index, int count) const;
  108.  
  109.     int __fastcall ToInt() const;
  110.     int __fastcall ToIntDef(int defaultValue) const;
  111.     double __fastcall ToDouble() const;
  112.  
  113.     // Convert to Unicode
  114.     int __fastcall WideCharBufSize() const;
  115.     wchar_t* __fastcall WideChar(wchar_t* dest, int destSize) const;
  116.  
  117.     // MBCS support
  118.     enum TStringMbcsByteType
  119.     {mbSingleByte, mbLeadByte, mbTrailByte};
  120.  
  121.     TStringMbcsByteType __fastcall ByteType(int index) const;
  122.     bool __fastcall IsLeadByte(int index) const;
  123.     bool __fastcall IsTrailByte(int index) const;
  124.     bool __fastcall IsDelimiter(const AnsiString& delimiters, int index) const;
  125.     bool __fastcall IsPathDelimiter(int index) const;
  126.     int  __fastcall LastDelimiter(const AnsiString& delimiters) const;
  127.     int  __fastcall AnsiPos(const AnsiString& subStr) const;
  128.     char* __fastcall AnsiLastChar() const;
  129.  
  130.   protected:
  131.     void  ThrowIfOutOfRange(int idx);
  132.  
  133.   private:
  134.     char *Data;
  135.   };
  136.  
  137.   extern PACKAGE AnsiString __fastcall operator +(const char*, const AnsiString& rhs);
  138.  
  139. #if defined(VCL_IOSTREAM)
  140.   ostream& operator << (ostream& os, const AnsiString& arg);
  141.   istream& operator >> (istream& is, AnsiString& arg);
  142. #endif
  143.  
  144.   template <unsigned char sz> class SmallStringBase
  145.   {
  146.   protected:
  147.     unsigned char Len;
  148.     char Data[sz];
  149.   };
  150.   
  151.   template <unsigned char sz> class SmallString : SmallStringBase<sz>
  152.   {
  153.  
  154. #if defined(VCL_IOSTREAM)
  155.     friend ostream& operator <<(ostream& os, const SmallString& arg)
  156.     {
  157.       os << AnsiString(arg);
  158.       return os;
  159.     }
  160.     friend istream& operator >>(istream& is, SmallString& arg)
  161.     {
  162.       AnsiString s;
  163.       is >> s;
  164.       arg = s;
  165.       return is;
  166.     }
  167. #endif
  168.  
  169.   public:
  170.     __fastcall SmallString() { Len = 0; }
  171.  
  172. #if 0
  173.     // Note: these constructors have been moved into the class delcaration
  174.     //       because the compiler doesn not like __fastcall on this syntax
  175.     //       when implemented outside the class delcaration (bug reported)
  176. #endif
  177.     __fastcall SmallString(const char* src)
  178.     {
  179.       long len = strlen(src);
  180.       Len = (unsigned char)((len > sz)? sz: len);
  181.       strncpy(Data, src, Len);
  182.     }
  183.     __fastcall SmallString(const SmallString& src)
  184.     {
  185. #pragma option push -w-inl
  186.       Len = src.Len;
  187.       for (int i = 0; i < Len; i++)
  188.         Data[i] = src.Data[i];
  189. #pragma option pop
  190.     }
  191.     __fastcall SmallString(const AnsiString& src)
  192.     {
  193.       long len = src.Length();
  194.       Len = (unsigned char)((len > sz)? sz: len);
  195.       strncpy(Data, src.c_str(), Len);
  196.     }
  197.  
  198.     char& __fastcall operator [](const unsigned char idx)
  199.     {return Data[idx-1];}
  200.  
  201.     SmallString& __fastcall operator =(const SmallString& rhs);
  202.  
  203.     __fastcall operator AnsiString() const;
  204.   };
  205.   
  206.   // used when SmallStrings are in unions (can't have a ctor)
  207.   // must cast DummySmallString to SmallString to do anything useful
  208.  
  209.   template<unsigned char sz> inline SmallString& __fastcall SmallString<sz>::operator =(const SmallString& rhs)
  210.   {
  211. #pragma option push -w-inl
  212.     if (this != &rhs)
  213.     {
  214.       Len = rhs.Len;
  215.       for (int i = 0; i < Len; i++)
  216.         Data[i] = rhs.Data[i];
  217.     }
  218.     return *this;
  219. #pragma option pop
  220.   }
  221.   
  222.   template<unsigned char sz> inline __fastcall SmallString<sz>::operator AnsiString() const
  223.   {
  224.     return AnsiString(Data, Len);
  225.   }
  226.  
  227. }
  228. using namespace System;
  229. #endif
  230.  
  231.   
  232.